package demo.spring.restful.controllers;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import ch.qos.logback.classic.Logger;
import demo.spring.restful.domain.Computer;
import demo.spring.restful.service.ComputerService;

@RestController
public class ComputerController {
	Logger logger = (Logger) LoggerFactory.getLogger(ComputerController.class);

	@Autowired
	private ComputerService computerService;

	@RequestMapping(value = "/computer/", method = RequestMethod.GET)
	public ResponseEntity<List<Computer>> listAllcomputers() {
		List<Computer> computers = computerService.findAllComputer();
		if (computers.isEmpty()) {
			return new ResponseEntity<List<Computer>>(HttpStatus.NO_CONTENT);

		}
		logger.info("获得电脑列表成功");
		return new ResponseEntity<List<Computer>>(computers, HttpStatus.OK);
	}

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Computer> getComputer(@PathVariable("id") long id) {
		System.out.println("Fetching computer with id " + id);
		Computer Computer = computerService.findById(id);
		if (Computer == null) {
			System.out.println("computer with id " + id + " not found");
			logger.info("所求电脑不存在");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}
		logger.info("获得所求电脑信息");
		return new ResponseEntity<Computer>(Computer, HttpStatus.OK);
	}

	@RequestMapping(value = "/computer/", method = RequestMethod.POST)
	public ResponseEntity<Void> createComputer(@RequestBody Computer computer, UriComponentsBuilder ucBuilder) {
		System.out.println("Creating computer: " + computer.getBrand() + "  " );
		if (computerService.isComputerExist(computer)) {
			System.out.println("A computer with brand " + computer.getBrand()  + " already exist");
			logger.info("所添加电脑已存在");
			return new ResponseEntity<Void>(HttpStatus.CONFLICT);
		}

		computerService.saveComputer(computer);

		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(ucBuilder.path("/computer/{id}").buildAndExpand(computer.getId()).toUri());
		logger.info("添加电脑成功");
		return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
	}

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.PUT)
	public ResponseEntity<Computer> updateComputer(@PathVariable("id") long id, @RequestBody Computer computer) {
		System.out.println("Updating computer " + id);

		Computer currentComputer = computerService.findById(id);

		if (currentComputer == null) {
			System.out.println("computer with id " + id + " not found");
			logger.info("所要修改的电脑不存在");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}

		currentComputer.setBrand(computer.getBrand());
		currentComputer.setCPU(computer.getCPU());


		computerService.updateComputer(currentComputer);
		logger.info("电脑信息修改成功");
		return new ResponseEntity<Computer>(currentComputer, HttpStatus.OK);
	}

	@RequestMapping(value = "/computer/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Computer> deleteComputer(@PathVariable("id") long id) {
		System.out.println("Fetching & Deleting Computer with id " + id);

		Computer Computer = computerService.findById(id);
		if (Computer == null) {
			System.out.println("Unable to delete. computer with id " + id + " not found");
			logger.info("所要删除的电脑不存在");
			return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
		}

		computerService.deleteComputerById(id);
		logger.info("所要删除的电脑信息删除成功");
		return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
	}

	@RequestMapping(value = "/computer/", method = RequestMethod.DELETE)
	public ResponseEntity<Computer> deleteAllcomputers() {
		System.out.println("Deleting All computers");

		computerService.deleteAllComputers();
		logger.info("删除电脑列表成功");
		return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
	}
}